# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: 线程池进程池练习.py
Author: kij
Create Date: 2021/3/20
-------------------------------------------------
"""
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
import os, time, random
from threading import currentThread
import requests
# def task(name):
# 	print('name=%s pid=%s'%(name,os.getpid()))
# 	time.sleep(random.randint(1,3))
#
# if __name__ == '__main__':
# 	pool = ProcessPoolExecutor(3)
# 	# pool = ThreadPoolExecutor(3)
# 	for item in range(10):
# 		pool.submit(task,'kij%s'%item)
#
# 	pool.shutdown(wait=True)
# 	print('主')

# def task():
# 	print('name:%s pid:%s run' % (currentThread().getName(), os.getpid()))
# 	time.sleep(random.randint(1, 3))
#
#
# if __name__ == '__main__':
# 	pool = ThreadPoolExecutor(3)
#
# 	for i in range(5):
# 		pool.submit(task, )
# 	# 关闭进程池入口 不让在添加任务，相当月join完成所有线程任务执行主进程
# 	pool.shutdown(wait=True)
#
# 	print('主')

def get(url):
	print('get%s' % url)
	response = requests.get(url)
	time.sleep(3)
	return {'url':url,'content':response.text}

def parse(res):
	res = res.result()
	print('%s parse res is %s ' %(res['url'],len(res['content'])))

if __name__ =='__main__':
	urls=[
		'https://shimo.im/sheets/hkYJ8pKjtpJRWWxP/vKqn9',
		'http://book.luffycity.com/python-book/di-8-zhang-mysql-shu-ju-ku/82-ku-cao-zuo/821-ku-cao-zuo.html',
		'https://pub.flutter-io.cn'
	]
	pool = ThreadPoolExecutor(2)

	for url in urls:
		pool.submit(get,url).add_done_callback(parse)